package com.boc.langxiang.presenter.common;


import com.boc.langxiang.contract.common.OtherLoginContract;
import com.boc.langxiang.entity.request.AuthLoginRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.UserInfo;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.HandleErrorSubscriber;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Li Guoxi on 2016/12/27.
 */

public class OtherLoginPresenter implements OtherLoginContract, OtherLoginContract.Presenter {
    private View mView;

    public OtherLoginPresenter(View view) {
        this.mView = view;
    }

    @Override
    public void authLogin(String lx, String openid) {
        mView.showLoading();
        AuthLoginRequest request = new AuthLoginRequest();
        request.lx = lx;
        request.openid = openid;
        mService.authLogin(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<UserInfo>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<UserInfo> response) {
                        UserInfo userInfo = response.getContent();
                        UserInfoManager.getInstance().setUserInfo(userInfo);
                        mView.onLoginSuccess();
                    }
                });
    }
}
